import java.util.ArrayList;
import java.util.List;

/**
 * @author LKQ
 * @date 2022/1/13 11:13
 * @description
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        solution2.findAnagrams("cbaebabacd", "abc");
    }
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new ArrayList<>();
        int slen = s.length();
        int plen = p.length();
        if (plen > slen) {
            return res;
        }
        int[] count = new int[26];
        for (char c : p.toCharArray()){
            count[c - 'a']++;
        }
        int l = 0, r = 0;
        char[] sc = s.toCharArray();

        while (r < slen){
            if (count[sc[r] - 'a'] > 0){
                count[sc[r++] - 'a']--;
                if (r - l == plen){
                    res.add(l);
                }
            }else {
                count[sc[l++] - 'a']++;
            }
        }
        return res;
    }
}
